package org.come.control;

import java.util.ArrayList;
import java.util.List;

import org.come.Frame.MsgJframe;
import org.come.Frame.RewardHallJframe;
import org.come.Frame.ZhuFrame;
import org.come.action.FromServerAction;
import org.come.bean.RewardDrawingBean;
import org.come.bean.RewardListBean;
import org.come.entity.Goodstable;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.image.ImageMixDeal;

public class RewardHallControl implements FromServerAction {

	@Override
	public void controlMessFromServer(String mes, String type) {
		// 依据协议头进行划分方法
		// String mesProcess[]=mes.split("//");

		switch (type) {
		case "obtainarticle":
			obtainArticle(mes);
			break;
		case "drawnitems":
			drawnItems(mes);
			break;
		case "drawnitemsfail":
			drawnitemsFail(mes);
			break;
		}

	}

	/**
	 * 获取赏功堂物品
	 */
	public void obtainArticle(String mes) {

		RewardListBean listReward = GsonUtil.getGsonUtil().getgson().fromJson(mes, RewardListBean.class);

		// 存到静态类里面
		UserMessUntil.setRewardHallArr(listReward.getRewardHalls());

		// 转成shop格式
		List<Goodstable> listGoods = new ArrayList<Goodstable>();
		for (int i = 0; i < UserMessUntil.getRewardHallArr().size(); i++) {

			Goodstable goodstable = GsonUtil.getGsonUtil().getgson().fromJson(UserMessUntil.getRewardHallArr().get(i).getGoodstable(), Goodstable.class);
			listGoods.add(i, goodstable);
		}

		RewardHallJframe.getRewardHallJframe().getRewardHallJpanel().showshop(listGoods);
		FormsManagement.showForm(59);

	}

	/**
	 * 抽中物品
	 */
	public void drawnItems(String mes) {

		RewardDrawingBean drawingBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, RewardDrawingBean.class);
		List<Goodstable> listGoods = new ArrayList<Goodstable>();
		Goodstable good = GsonUtil.getGsonUtil().getgson().fromJson(drawingBean.getRewardHall().getGoodstable(), Goodstable.class);
		// 判断是添加，还是删除物品
		if (drawingBean.getRoleName() == null || "".equals(drawingBean.getRoleName())) { // 投放物品

			// 新加的物品
			UserMessUntil.getRewardHallArr().add(drawingBean.getRewardHall());
			for (int i = 0; i < UserMessUntil.getRewardHallArr().size(); i++) {

				Goodstable goodstable = GsonUtil.getGsonUtil().getgson().fromJson(UserMessUntil.getRewardHallArr().get(i).getGoodstable(), Goodstable.class);
				listGoods.add(i, goodstable);
			}
			// 刷新界面
			RewardHallJframe.getRewardHallJframe().getRewardHallJpanel().showshop(listGoods);

		} else {// 删除抽奖物品
				// 判断是否是自己
			if (ImageMixDeal.userimg.getRoleShow().getRolename().equals(drawingBean.getRoleName())) {// 是自己
				// 中奖特效
				MsgJframe.getJframe().getJapnel().texiao("lottery");
				// 提示抽中了什么物品
				ZhuFrame.getZhuJpanel().addPrompt("恭喜您抽中了1个" + good.getGoodsname());
			}
			int exi = -1;
			for (int i = 0; i < UserMessUntil.getRewardHallArr().size(); i++) {
				if (UserMessUntil.getRewardHallArr().get(i).getId().compareTo(drawingBean.getRewardHall().getId()) == 0) {
					exi = i;
				}
			}
			if (exi != -1) {
				// 删除的物品
				UserMessUntil.getRewardHallArr().remove(exi);
			}
			for (int i = 0; i < UserMessUntil.getRewardHallArr().size(); i++) {
				Goodstable goodstable = GsonUtil.getGsonUtil().getgson().fromJson(UserMessUntil.getRewardHallArr().get(i).getGoodstable(), Goodstable.class);
				listGoods.add(i, goodstable);
			}
			// 刷新界面
			RewardHallJframe.getRewardHallJframe().getRewardHallJpanel().showshop(listGoods);
		}
	}

	/**
	 * 抽奖失败
	 */
	public void drawnitemsFail(String mes) {

		if (mes.equals(0)) {// 0没抽中
			// 关闭抽奖面板
			FormsManagement.HideForm(59);
			ZhuFrame.getZhuJpanel().addPrompt2("您的手速不够快啊,再接再励!!");
		} else {// 还剩多长时间可以再抽
			int day = Integer.parseInt(mes);
			ZhuFrame.getZhuJpanel().addPrompt2("您还有" + (7 - day) + "天才能再次抽奖!!");
		}
	}

}
